package com.ts.main.mini.service;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.ts.main.mini.context.UserContext;
import com.ts.main.mini.dto.ShoppingCartDto;
import com.ts.main.mini.dto.ShoppingCartPageSearchDto;
import com.ts.main.mini.entity.ShoppingCartEntity;
import com.ts.main.mini.exception.RPage;
import com.ts.main.mini.mapper.ShoppingCartMapper;
import com.ts.main.mini.vo.ShoppingCartVo;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author lk
 * @description 针对表【t_shopping_cart(用户购物车表)】的数据库操作Service实现
 * @createDate 2025-05-16 15:12:01
 */
@AllArgsConstructor
@Service
public class ShoppingCartServiceImpl extends ServiceImpl<ShoppingCartMapper, ShoppingCartEntity> {

    public RPage<List<ShoppingCartVo>> getCartPage(ShoppingCartPageSearchDto dto) {
        Integer userId = UserContext.getUser().getUserId();
        dto.setUserId(userId);
        Page<Object> page = PageHelper.startPage(dto.getPage(), dto.getLimit());
        List<ShoppingCartVo> list = baseMapper.getCartPage(dto.getUserId());
        return RPage.ok(list, dto.getPage(), dto.getLimit(), page.getTotal());
    }

    public List<ShoppingCartVo> getCartList() {
        Integer userId = UserContext.getUser().getUserId();
        return baseMapper.getCartList(userId);
    }

    public ShoppingCartVo getCartDetails(Integer cartId) {
        return baseMapper.getCartDetails(cartId);
    }

    @Transactional
    public Boolean saveCart(ShoppingCartDto dto) {
        Integer userId = UserContext.getUser().getUserId();
        dto.setUserId(userId);
        //购物车是否存在该商品，累加或新增
        LambdaQueryWrapper<ShoppingCartEntity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper
                .eq(ShoppingCartEntity::getUserId, userId)
                .eq(ShoppingCartEntity::getVariantId, dto.getVariantId())
                .eq(ShoppingCartEntity::getIsDeleted,false);
        ShoppingCartEntity cartEntity = getOne(queryWrapper);
        if(ObjectUtil.isEmpty(cartEntity)) {
            return save(BeanUtil.copyProperties(dto, ShoppingCartEntity.class));
        }else {
            cartEntity.setProductCount(cartEntity.getProductCount() + dto.getProductCount());
            return updateById(cartEntity);
        }
    }

    public Boolean updateCart(ShoppingCartDto dto) {
        ShoppingCartEntity entity = BeanUtil.copyProperties(dto, ShoppingCartEntity.class);
        return updateById(entity);
    }

    public Boolean deleteCart(List<Integer> ids) {
        LambdaUpdateWrapper<ShoppingCartEntity> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.set(ShoppingCartEntity::getIsDeleted, true)
                .in(ShoppingCartEntity::getCartId, ids);
        return update(updateWrapper);
    }

}




